#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: 
@time: 2024/2/28 9:06 PM
@Function: 请输入模块功能描述
"""
import os.path

import openpyxl


class Reader:
    """
    pip install openpyxl
    """
    def __init__(self):
        self.workbook=None#工作簿
        self.sheet=None#表格下的sheet页
        self.rows=0#表示具体的行数
        self.r=0#当前读取到的行数

    def open_excel(self,srcfile):
        if not os.path.isfile(srcfile):
            print("{} not exist".format(srcfile))
            return
        openpyxl.workbook.encoding='utf-8'
        self.workbook=openpyxl.load_workbook(filename = srcfile)
        self.sheet=self.workbook[self.workbook.sheetnames[0]]
        self.rows=self.sheet.max_row
        self.r=0
        return

    def get_sheets(self):
        sheets=self.workbook.sheetnames
        return sheets

    def set_sheet(self,name):
        """
        切换sheet页
        :return:
        :name:sheet页的名称
        """
        self.sheet=self.workbook[name]
        self.rows=self.sheet.max_row
        self.r=0
        return

    def readline(self):
        """
        逐行读取
        :return:
        """
        lines=[]
        for row in self.sheet.rows:
            line=[]
            for cell in row:
                if cell.value is None:
                    line.append("")
                else:
                    line.append(cell.value)
            lines.append(line)
        return lines

